//
//  XZTrainTool.m
//  生活小助手
//
//  Created by lixiangzhou on 14-8-7.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "XZTrainTool.h"
#import "XZHttpTool.h"
#import "MJExtension.h"
#import "XZTrainNumResult.h"
#import "XZSTSResult.h"
#import "XZLTResult.h"
/*
 202201	车次不能为空
 202202	查询不到车次的相关信息
 202203	出发站或终点站不能为空
 202204	查询不到结果
 */

@implementation XZTrainTool

+ (void)trainNumWithParameter:(id)parameter success:(void (^)(XZTrainNumResult *))success failure:(void (^)(NSError *))failure
{
    NSDictionary *dictParam = [parameter keyValues];
    
    [XZHttpTool getWithUrl:@"http://apis.juhe.cn/train/s" parameters:dictParam success:^(NSDictionary *responseObj) {
        int errorcode = [responseObj[@"error_code"] intValue];
        if (errorcode) {
            NSString *domain;
            if (errorcode == 202201) {
                domain = @"车次不能为空";
            } else if (errorcode == 202202) {
                domain = @"查询不到车次的相关信息";
            } else if (errorcode == 202203) {
                domain = @"出发站或终点站不能为空";
            } else if (errorcode == 202204) {
                domain = @"查询不到结果";
            } else if (errorcode == 202207) {
                domain = @"查询不到结果";
            }
            NSError *ipError = [NSError errorWithDomain:domain code:errorcode userInfo:nil];
            if (failure) {
                failure(ipError);
            }
        } else {
            id resultObj = [[XZTrainNumResult class] objectWithKeyValues:responseObj];
            if (success) {
                success(resultObj);
            }
        }
    } failure:^(NSError *error) {
        NSError *trainError = [NSError errorWithDomain:@"网络无连接" code:[error code] userInfo:error.userInfo];
        if (failure) {
            failure(trainError);
        }
    }];
}

+ (void)stationToStationWithParameter:(id)param success:(void (^)(XZSTSResult *))success failure:(void (^)(NSError *))failure
{
    NSDictionary *dictParam = [param keyValues];
    [XZHttpTool getWithUrl:@"http://apis.juhe.cn/train/s2s" parameters:dictParam success:^(id responseObj) {
        int errorcode = [responseObj[@"error_code"] intValue];
        if (errorcode) {
            NSString *domain;
            if (errorcode == 202201) {
                domain = @"车次不能为空";
            } else if (errorcode == 202202) {
                domain = @"查询不到车次的相关信息";
            } else if (errorcode == 202203) {
                domain = @"出发站或终点站不能为空";
            } else if (errorcode == 202204) {
                domain = @"查询不到结果";
            } else if (errorcode == 202207) {
                domain = @"查询不到结果";
            }
            NSError *ipError = [NSError errorWithDomain:domain code:errorcode userInfo:nil];
            if (failure) {
                failure(ipError);
            }
        } else {
            id resultObj = [[XZSTSResult class] objectWithKeyValues:responseObj];
            if (success) {
                success(resultObj);
            }
        }
    } failure:^(NSError *error) {
        NSError *trainError = [NSError errorWithDomain:@"网络无连接" code:[error code] userInfo:error.userInfo];
        if (failure) {
            failure(trainError);
        }
    }];
}

+ (void)leftTicketWithParameter:(id)param success:(void (^)(XZLTResult *))success failure:(void (^)(NSError *))failure
{
    NSDictionary *dictParam = [param keyValues];
    [XZHttpTool getWithUrl:@"http://apis.juhe.cn/train/yp" parameters:dictParam success:^(id responseObj) {
        int errorcode = [responseObj[@"error_code"] intValue];
        if (errorcode) {
            NSString *domain;
            if (errorcode == 202201) {
                domain = @"车次不能为空";
            } else if (errorcode == 202202) {
                domain = @"查询不到车次的相关信息";
            } else if (errorcode == 202203) {
                domain = @"出发站或终点站不能为空";
            } else if (errorcode == 202204) {
                domain = @"查询不到结果";
            } else if (errorcode == 202207) {
                domain = @"查询不到结果";
            }
            NSError *ipError = [NSError errorWithDomain:domain code:errorcode userInfo:nil];
            if (failure) {
                failure(ipError);
            }
        } else {
            id resultObj = [[XZLTResult class] objectWithKeyValues:responseObj];
            if (success) {
                success(resultObj);
            }
        }
    } failure:^(NSError *error) {
        NSError *trainError = [NSError errorWithDomain:@"网络无连接" code:[error code] userInfo:error.userInfo];
        if (failure) {
            failure(trainError);
        }
    }];
}

@end
